<?php
namespace frontend\controllers;

use Yii;
use PDO;
use yii\web\Controller;

class IndexController extends Controller
{
	public $enableCsrfValidation = false;
	/*
	 * ½ûÓÃyiiÑùÊ½
	 */
	public $layout = false;

	/**
     * ¹«¹²Á´½ÓPDO
     */
	public function link_pdo($k){
        $dbinfos = Yii::$app->session->get('dbinfos');
        $dbinfos_arr = unserialize($dbinfos);
        $one_dbinfo = $dbinfos_arr[$k];
        $dbh = new PDO("mysql:host=$one_dbinfo[1];port=$one_dbinfo[2];charset=utf8", "$one_dbinfo[3]", "$one_dbinfo[4]");
        return $dbh;
    }
	
	/*
	 * Ê×Ò³
	 */
	public function actionIndex(){
	    $dbinfos = Yii::$app->session->get('dbinfos');
	    $dbinfos_arr = unserialize($dbinfos);
	    if(count($dbinfos_arr) === 0 || $dbinfos_arr === false){
	        $dbinfos_arr = [];
	    }
		return $this->render('index', [
			'dbinfos_arr' => $dbinfos_arr // ËùÓÐÁ´½Ó
		]);
	}

	/**
	 * ²é¿´µ±Ç°¿âÃû --- ÕâÀï´æÒ»´Î
	 */
	public function actionDbnames(){
		$k = Yii::$app->request->post('k');
		Yii::$app->session->set('link_k', $k); //½«k´æÈësession
        $dbh = $this->link_pdo($k);
        //»ñÈ¡Êý¾Ý¿âËùÓÐ¿âÃû
        $all_dbname = "show databases";
        $dbnames = $dbh->query($all_dbname)->fetchAll(PDO::FETCH_ASSOC); 
        if(count($dbnames) === 0 || $dbnames === false){
            $dbnames = [];
        }
        $databases = array_column($dbnames, 'Database');
        return json_encode($databases);
	}

	/*
	 * ²é¿´µ±Ç°¿âÏÂµÄËùÓÐ±íÃû
	 */
	public function actionTablenames(){
		$k = Yii::$app->session->get('link_k');
		$at_dbname = Yii::$app->request->get('at_dbname'); //µ±Ç°Êý¾Ý¿âÃû³Æ
		Yii::$app->session->set('dbname',$at_dbname);
		// $at_dbname = 'information_schema';
		$dbinfos = Yii::$app->session->get('dbinfos');
        $dbinfos_arr = unserialize($dbinfos);
        $one_dbinfo = $dbinfos_arr[$k];
        $dbh = new PDO("mysql:host=$one_dbinfo[1];port=$one_dbinfo[2];dbname=$at_dbname;charset=utf8", "$one_dbinfo[3]", "$one_dbinfo[4]");

        //»ñÈ¡µ±Ç°Êý¾Ý¿âËùÓÐ±íÃû
        $all_tablename =  "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$at_dbname'";
        $tablenames = $dbh->query($all_tablename)->fetchAll(PDO::FETCH_ASSOC);
        $arr1 = count($tablenames);
        $size=10;
		$ye_shu=ceil($arr1/$size);
		$p=isset($_GET['p'])?$_GET['p']:1;
		$shang=$p-1<1?1:$p-1;
		$xia=$p+1>$ye_shu?$ye_shu:$p+1;
		$limit=($p-1)*$size;
		$all_tablenames =  "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$at_dbname' limit $limit,$size";
        $tablenamess = $dbh->query($all_tablenames)->fetchAll(PDO::FETCH_ASSOC);
        foreach ($tablenamess as $k => $v) {
        	$table = $v['TABLE_NAME'];
        	$count = "select count(*) from $table";
        	$sum = $dbh->query($count)->fetchAll(PDO::FETCH_ASSOC);
        	$tablenamess[$k]['sum'] = $sum[0]['count(*)'];
        }
        // print_r($tablenamess);die;
	    if(count($dbinfos_arr) === 0 || $dbinfos_arr === false){
	        $dbinfos_arr = [];
	    }
        return $this->render('table_show', [
        	'table'  => $tablenamess,
        	'at_dbname'  => $at_dbname,
        	'xia'=>$xia,
        	'shang'=>$shang,
        	'ye_shu'=>$ye_shu,
        	'p'=>$p
        ]);
	}
	/*
	 * Ê×Ò³Ç¶Ì×Ò³Ãæ
	 */
	public function actionIndex_v1(){
		return $this->render('index_v1');
	}


}